//-------------------------------------------------------------------------
//  PP10_4Sorting.java   Author: Tang Caiming
//
//  重写Sorting类，使排序算法以递减的顺序排序
//-------------------------------------------------------------------------
public class PP10_4Sorting {
    public static void selectionSort(Comparable[] list)
    {
        int max;
        Comparable temp;

        for (int index = 0; index < list.length-1; index++)
        {
            max = index;
            for (int scan = index+1; scan < list.length; scan++)
                if (list[scan].compareTo(list[max]) > 0)
                    max = scan;

            // Swap the values
            temp = list[max];
            list[max] = list[index];
            list[index] = temp;
        }
    }


    public static void insertionSort(Comparable[] list)
    {
        for (int index = 1; index < list.length; index++)
        {
            Comparable key = list[index];
            int position = index;

            //  Shift larger values to the right
            while (position > 0 && key.compareTo(list[position-1]) < 0)
            {
                list[position] = list[position-1];
                position--;
            }

            list[position] = key;
        }
    }
}

//-------------------------------------------------------------------------
//  PhoneList.java
//
//  测试驱动重写后的Sorting类，使排序算法以递减的顺序排序
//-------------------------------------------------------------------------
class PhoneList
{
    //-----------------------------------------------------------------
    //  Creates an array of Contact objects, sorts them, then prints
    //  them.
    //-----------------------------------------------------------------
    public static void main(String[] args)
    {
        Contact[] friends = new Contact[8];

        friends[0] = new Contact("John", "Smith", "610-555-7384");
        friends[1] = new Contact("Sarah", "Barnes", "215-555-3827");
        friends[2] = new Contact("Mark", "Riley", "733-555-2969");
        friends[3] = new Contact("Laura", "Getz", "663-555-3984");
        friends[4] = new Contact("Larry", "Smith", "464-555-3489");
        friends[5] = new Contact("Frank", "Phelps", "322-555-2284");
        friends[6] = new Contact("Mario", "Guzman", "804-555-9066");
        friends[7] = new Contact("Marsha", "Grant", "243-555-2837");

        PP10_4Sorting.selectionSort(friends);

        for (Contact friend : friends)
            System.out.println(friend);
    }
}
